package batch;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import javax.mail.MessagingException;

import constant.Constant;
import dto.DTOAbsence;
import dto.DTOContact;
import business.BusinessImpl;
import tools.MailHelper;

public class Batch {

	private BusinessImpl businessImpl = new BusinessImpl();
	
	
  public static void main(String[] args) {
//	   final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
//	    scheduler.scheduleAtFixedRate(new Runnable() {
//			
//			@Override
//			public void run() {
//				try {
//					new Batch();
//				} catch (IOException e) {
//				
//					e.printStackTrace();
//				}
//			}
//		}, 0, 1, TimeUnit.DAYS);
	    
	  try {
		new Batch();
	} catch (IOException e) {
		e.printStackTrace();
	}	  
 
  }

  public Batch() throws IOException {


	  
	 Calendar cal = Calendar.getInstance();
	 
	 for (DTOAbsence dtoAbsence : businessImpl.getDailyAbsence(cal)) {
		if(!dtoAbsence.isMailSent()){
			
			DTOContact contactMissing = businessImpl.getContactById(dtoAbsence.getContact_idcontact());
			
			Calendar AbsenceDate = Calendar.getInstance();
			AbsenceDate.setTime(dtoAbsence.getDate());
			
			
			
			 try {
			 
				 
				 String content = null;
				 
				 BufferedReader reader = null;
	
				 try {
						reader = new BufferedReader(new FileReader("mailAbsence.html"));
						 String line = null;
						 
						 while ((line = reader.readLine()) != null) {
			
							for (String htmlVariable : Constant.HTMLVARIABLE) {
							
								 // find all occurrences backward
								 for (int i = line.length(); (i = line.lastIndexOf(htmlVariable, i - 1)) != -1; ) {
								     String templine = line.substring(0, i);
								     
								    if (htmlVariable == "{APPRENTI_NOM}") {
								    	templine += contactMissing.getLastname();
									}else if (htmlVariable == "{APPRENTI_PRENOM}") {
										templine += contactMissing.getFirstname();
									}else if (htmlVariable == "{REMARQUE}") {
										templine += dtoAbsence.getComment();
									}else if (htmlVariable == "{DATE}") {
										templine += Constant.gettextdate(AbsenceDate);
									}else if (htmlVariable == "{PERIODE}") {
										templine += dtoAbsence.getPeriod();
									}
														     								     
								    templine += line.substring(i+htmlVariable.length());
								    line = templine;
								 }
								 
								
							}			 
						
							   	content += line;   	
						   	
						 }
				} catch (FileNotFoundException e) {
						e.printStackTrace();
				} finally {
		            if (reader != null) 
		            	reader.close();
		         }      
					
				
					 
			 
				  MailHelper.sendMail("Avis absence", contactMissing.getEmail(), "[EPTSion] Avis d'absence", content);
			    } catch (MessagingException e) {
			      e.printStackTrace();
				} 

			 	dtoAbsence.setMailSent(true);
			 
			 	businessImpl.updateAbsence(contactMissing, AbsenceDate, dtoAbsence);
		}
	}

   
  }
}
